//
//  UIImage+ZHExtension.m
//  KLCategory
//
//  Created by zhusanbao on 2020/10/19.
//

#import "UIImage+ZHExtension.h"

@implementation UIImage (ZHExtension)

- (UIImage *)stretchImage{
    return [self stretchImageOffset:CGSizeZero];
}

- (UIImage *)stretchImageOffset:(CGSize)offset{
    CGSize size = self.size;
    return [self resizableImageWithCapInsets:UIEdgeInsetsMake(size.height*0.5-1+offset.height, size.width*0.5-1+offset.width, size.height*0.5+1+offset.height, size.width*0.5+1+offset.width) resizingMode:UIImageResizingModeTile];
}
-(UIImage *)changeGrayImage:(UIImage *)oldImage
{
    int bitmapInfo = kCGImageAlphaNone;
    int width = oldImage.size.width;
    int height = oldImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,
                                                  width,
                                                  height,
                                                  8,
                                                  0,
                                                  colorSpace,
                                                  bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,
                       CGRectMake(0, 0, width, height), oldImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
}

+ (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tintColor renderingMode:(UIImageRenderingMode)mode{
    if (@available(iOS 13.0, *)) {
        return [[UIImage imageNamed:name] imageWithTintColor:tintColor renderingMode:mode];
    } else {
        return [UIImage imageNamed:name];
    }
}

+ (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tintColor{
    if (@available(iOS 13.0, *)) {
        return [[UIImage imageNamed:name] imageWithTintColor:tintColor];
    } else {
        return [UIImage imageNamed:name];
    }
}

+ (UIImage *)imageWithContentsOfFile:(NSString *)path tint:(UIColor *)tintColor{
    if (@available(iOS 13.0, *)) {
        return [[UIImage imageWithContentsOfFile:path] imageWithTintColor:tintColor];
    } else {
        return [UIImage imageWithContentsOfFile:path];
    }
}

@end
